const { ref } = Vue
const { ElLoading, ElMessage } = ElementPlus

export async function useFetch(config) {
  const data = ref(null);
  const total = ref(null);
  const loading = ElLoading.service({ lock: true, text: '加载中', customClass: 'custom-loading', background: 'rgba(255, 255, 255, 0.2)' });
  const { data: { ReturnData: fetchData, RowCount: totalCount, Code, Message } } = await axios({
    url: config.url,
    method: 'post',
    data: config.data
  })
  loading.close();
  if (parseInt(Code) === 500) {
    ElMessage.error(Message);
    data.value = null;
    total.value = null;
  } else {
    data.value = fetchData;
    total.value = parseInt(totalCount);
  }
  return { data, total, status: { code: parseInt(Code), msg: Message } };
}